<?php

# http://projects:8081/cdapp-2kt-api/api/agent_ntfm_init.php
# http://projects:8081/cdapp-2kt-api/api/agent_ntfm_init.php?dev=1&mod=ap

# const http://172.28.196.103:9000/NTFM-FORE/forecast/sect/queryClassifyFlow;
# const http://172.28.196.103:9000/NTFM-FORE/forecast/port/queryClassifyFlow;

include_once '_portal.php';

$dev = intval($_GET['dev'] ?? 0);
$mod = intval($_GET['mod'] ?? '*');
$force = intval($_GET['force'] ?? ($_GET['f'] ?? 0));
define('FORCE', $force);

############################################################

$tma_tx = init_param('NTFM_TMA_TIMESTAMP_SEND', 0);
$tma_rx = init_param('NTFM_TMA_TIMESTAMP_RECIVE', 0);
$tma_min = init_param('NTFM_TMA_INTERVAL_MIN', 60000);
$tma_max = init_param('NTFM_TMA_INTERVAL_MAX', 120000);

$ap_tx = init_param('NTFM_AP_TIMESTAMP_SEND', 0);
$ap_rx = init_param('NTFM_AP_TIMESTAMP_RECIVE', 0);
$ap_min = init_param('NTFM_AP_INTERVAL_MIN', 180000);
$ap_max = init_param('NTFM_AP_INTERVAL_MAX', 300000);

$div_tx = init_param('NTFM_DIV_TIMESTAMP_SEND', 0);
$div_rx = init_param('NTFM_DIV_TIMESTAMP_RECIVE', 0);
$div_min = init_param('NTFM_DIV_INTERVAL_MIN', 180000);
$div_max = init_param('NTFM_DIV_INTERVAL_MAX', 300000);

$nrs_tx = init_param('NTFM_NRS_TIMESTAMP_SEND', 0);
$nrs_rx = init_param('NTFM_NRS_TIMESTAMP_RECIVE', 0);
$nrs_min = init_param('NTFM_NRS_INTERVAL_MIN', 180000);
$nrs_max = init_param('NTFM_NRS_INTERVAL_MAX', 300000);

############################################################

if ($dev == 1) {
  $output = [];
  if ($mod == 'tma' || $mod == '*') {
    $output['payload_ap'] = get_payload_tma();
  }
  if ($mod == 'ap' || $mod == '*') {
    $output['payload_ap'] = get_payload_ap();
  }
  output($output);
  die();
}

$result = ['status' => 1];
$result_tma = ntfm_verify($tma_tx, $tma_rx, $tma_min, $tma_max);
$result_ap = ntfm_verify($ap_tx, $ap_rx, $ap_min, $ap_max);
$result_div = ntfm_verify($div_tx, $div_rx, $div_min, $div_max);
$result_nrs = ntfm_verify($nrs_tx, $nrs_rx, $nrs_min, $nrs_max);

############################################################

$result['status_tma'] = $result_tma['status'];
$result['msg_tma'] = $result_tma['msg'];
$result['payload_tma'] = '';
if ($result_tma['status'] > 0) {
  $payload_tma = get_payload_tma();
  $result['payload_tma'] = $payload_tma;
  param('NTFM_TMA_TIMESTAMP_SEND', timestamp());
  param('NTFM_TMA_TIME_SEND', date('Y-m-d H:i:s'));
  param('NTFM_TMA_PAYLOAD_LAST', $payload_tma);
}

############################################################

$result['status_ap'] = $result_ap['status'];
$result['msg_ap'] = $result_ap['msg'];
$result['payload_ap'] = '';
if ($result_ap['status'] > 0) {
  $payload_ap = get_payload_ap();
  $result['payload_ap'] = $payload_ap;
  param('NTFM_AP_TIMESTAMP_SEND', timestamp());
  param('NTFM_AP_TIME_SEND', date('Y-m-d H:i:s'));
  param('NTFM_AP_PAYLOAD_LAST', $payload_ap);
}

############################################################

$result['status_div'] = $result_div['status'];
$result['msg_div'] = $result_div['msg'];
$result['payload_div'] = '';
if ($result_div['status'] > 0) {
  $payload_div = get_payload_div();
  $result['payload_div'] = $payload_div;
  param('NTFM_DIV_TIMESTAMP_SEND', timestamp());
  param('NTFM_DIV_TIME_SEND', date('Y-m-d H:i:s'));
  param('NTFM_DIV_PAYLOAD_LAST', $payload_div);
}

############################################################

$result['status_nrs'] = $result_nrs['status'];
$result['msg_nrs'] = $result_nrs['msg'];
$result['payload_nrs'] = '';
if ($result_nrs['status'] > 0) {
  $payload_nrs = get_payload_nrs();
  $result['payload_nrs'] = $payload_nrs;
  param('NTFM_NRS_TIMESTAMP_SEND', timestamp());
  param('NTFM_NRS_TIME_SEND', date('Y-m-d H:i:s'));
  param('NTFM_NRS_PAYLOAD_LAST', $payload_nrs);
}

############################################################
output($result);
die();

function ntfm_verify($tx, $rx, $min, $max) {
  // 无param的情况
  // if(empty()){}
  $t0 = timestamp();
  if ($t0 - $rx >= $max) {
    return [
      'status' => FORCE ? 99 : 2,
      'msg' => (FORCE ? 'Forced: ' : '') . 'Need to load, exceeded max interval.',
    ];
  } elseif ($tx > $rx) {
    return [
      'status' => FORCE ? 99 : -1,
      'msg' => (FORCE ? 'Forced: ' : '') . 'No need to load, waiting for response.',
    ];
  } elseif ($t0 - $rx < $min) {
    return [
      'status' => FORCE ? 99 : 0,
      'msg' => (FORCE ? 'Forced: ' : '') . 'No need to load.',
    ];
  } else {
    return [
      'status' => FORCE ? 99 : 1,
      'msg' => (FORCE ? 'Forced: ' : '') . 'Need to load.',
    ];
  }
}

function get_payload_tma() {
  $payload = param('NTFM_TMA_PAYLOAD');
  $payload['step'] = param('NTFM_TMA_STEP_MINUTES');
  $payload['begTime'] = date('Y-m-d H', (time() - (3600 * param('NTFM_TMA_LOAD_HOURS_BEFORE')))) . ':00:00';
  $payload['endTime'] = date('Y-m-d H', (time() + (3600 * param('NTFM_TMA_LOAD_HOURS_AFTER')))) . ':00:00';
  // return ['payload' => json_stringify($payload), 'begtime' => $begtime, 'endtime' => $endtime];
  return $payload;
}

function get_payload_div() {
  // $begTime = rawurlencode('2024-06-15 00:00:00');
  // $endTime = rawurlencode('2024-06-19 00:00:00');
  $begTime = rawurlencode(date('Y-m-d', strtotime('yesterday')) . ' 00:00:00');
  $endTime = rawurlencode(date('Y-m-d', strtotime('tomorrow')) . ' 23:59:59');

  $adep = implode(',', ['ZUUU', 'ZUTF', 'ZUMY', 'ZUYB', 'ZULZ']);
  $payload = "?begTime={$begTime}&endTime={$endTime}&adep={$adep}";
  return $payload;
}

function get_payload_ap() {
  $payload = init_param('NTFM_AP_PAYLOAD', json_stringify([
    'ZUUU' => [
      'item' => 'ZUUU',
      'classContent' => "(adep in ('ZUUU')) and (route_ana like '%;BOKIR;%'):(adep in ('ZUUU')) and (route_ana like '%;GURET;%'):(adep in ('ZUUU')) and (route_ana like '%;UBRAB;%'):(adep in ('ZUUU')) and (route_ana like '%;ATVAX;%'):(adep in ('ZUUU')) and (route_ana like '%;IDBOR;%'):(adep in ('ZUUU')) and (route_ana like '%;LUVEN;%'):(adep in ('ZUUU')) and (route_ana like '%;MUMGO;%'):(ades in ('ZUUU')) and (route_ana like '%;MEXAD;%'):(ades in ('ZUUU')) and (route_ana like '%;AKDIK;%'):(ades in ('ZUUU')) and (route_ana like '%;EKOKA;%'):(ades in ('ZUUU')) and (route_ana like '%;IGNAK;%'):(ades in ('ZUUU')) and (route_ana like '%;LADUP;%'):other",
      'key' => 'ZUUU_DEP_BOKIR:ZUUU_DEP_GURET:ZUUU_DEP_UBRAB:ZUUU_DEP_ATVAX:ZUUU_DEP_IDBOR:ZUUU_DEP_LUVEN:ZUUU_DEP_MUMGO:ZUUU_ARR_MEXAD:ZUUU_ARR_AKDIK:ZUUU_ARR_EKOKA:ZUUU_ARR_IGNAK:ZUUU_ARR_LADUP:ZUUU_OTHER',
    ],
    'ZUTF' => [
      'item' => 'ZUTF',
      'classContent' => "(adep in ('ZUTF')) and (route_ana like '%;BOKIR;%'):(adep in ('ZUTF')) and (route_ana like '%;SAGPI;%'):(adep in ('ZUTF')) and (route_ana like '%;UBRAB;%'):(adep in ('ZUTF')) and (route_ana like '%;ATVAX;%'):(adep in ('ZUTF')) and (route_ana like '%;LUVEN;%'):(adep in ('ZUTF')) and (route_ana like '%;MUMGO;%'):(ades in ('ZUTF')) and (route_ana like '%;MEXAD;%'):(ades in ('ZUTF')) and (route_ana like '%;BUPMI;%'):(ades in ('ZUTF')) and (route_ana like '%;AKOPI;%'):(ades in ('ZUTF')) and (route_ana like '%;ELDUD;%'):(ades in ('ZUTF')) and (route_ana like '%;IGNAK;%'):(ades in ('ZUTF')) and (route_ana like '%;LADUP;%'):other",
      'key' => 'ZUTF_DEP_BOKIR:ZUTF_DEP_SAGPI:ZUTF_DEP_UBRAB:ZUTF_DEP_ATVAX:ZUTF_DEP_LUVEN:ZUTF_DEP_MUMGO:ZUTF_ARR_MEXAD:ZUTF_ARR_BUPMI:ZUTF_ARR_AKOPI:ZUTF_ARR_ELDUD:ZUTF_ARR_IGNAK:ZUTF_ARR_LADUP:ZUTF_OTHER',
    ],
    'ZUMY' => [
      'item' => 'ZUMY',
      'classContent' => "(adep in ('ZUMY')) and (route_ana like '%;BOKIR;%'):(adep in ('ZUMY')) and (route_ana like '%;SAGPI;%'):(adep in ('ZUMY')) and (route_ana like '%;UBRAB;%'):(adep in ('ZUMY')) and (route_ana like '%;JTG;%'):(ades in ('ZUMY')) and (route_ana like '%;MEXAD;%'):(ades in ('ZUMY')) and (route_ana like '%;AKDIK;%'):(ades in ('ZUMY')) and (route_ana like '%;BUPMI;%'):(ades in ('ZUMY')) and (route_ana like '%;IGNAK;%'):(ades in ('ZUMY')) and (route_ana like '%;LADUP;%'):other",
      'key' => 'ZUMY_DEP_BOKIR:ZUMY_DEP_SAGPI:ZUMY_DEP_UBRAB:ZUMY_DEP_JTG:ZUMY_ARR_MEXAD:ZUMY_ARR_AKDIK:ZUMY_ARR_BUPMI:ZUMY_ARR_IGNAK:ZUMY_ARR_LADUP:ZUMY_OTHER',
    ],
    'ZUYB' => [
      'item' => 'ZUYB',
      'classContent' => "(adep in ('ZUYB')) and (route_ana like '%;IGNAK;%'):(adep in ('ZUYB')) and (route_ana like '%;KAKMI;%'):(adep in ('ZUYB')) and (route_ana like '%;XYO;%'):(ades in ('ZUYB')) and (route_ana like '%;IGUNI;%'):(ades in ('ZUYB')) and (route_ana like '%;OTGIN;%'):(ades in ('ZUYB')) and (route_ana like '%;VEDPO;%'):(ades in ('ZUYB')) and (route_ana like '%;KAKMI;%'):other",
      'key' => 'ZUYB_DEP_IGNAK:ZUYB_DEP_KAKMI:ZUYB_DEP_XYO:ZUYB_ARR_IGUNI:ZUYB_ARR_OTGIN:ZUYB_ARR_VEDPO:ZUYB_ARR_KAKMI:ZUYB_OTHER',
    ],
    'ZULZ' => [
      'item' => 'ZULZ',
      'classContent' => "(adep in ('ZULZ')) and (route_ana like '%;WLY;%'):(adep in ('ZULZ')) and (route_ana like '%;OJG;%'):(adep in ('ZULZ')) and (route_ana like '%;NIXID;%'):(ades in ('ZULZ')) and (route_ana like '%;OTGIN;%'):(ades in ('ZULZ')) and (route_ana like '%;ANLAM;%'):(ades in ('ZULZ')) and (route_ana like '%;OJG;%'):(ades in ('ZULZ')) and (route_ana like '%;XYO;%'):(ades in ('ZULZ')) and (route_ana like '%;WLY;%'):other",
      'key' => 'ZULZ_DEP_WLY:ZULZ_DEP_OJG:ZULZ_DEP_NIXID:ZULZ_ARR_IGUNI:ZULZ_ARR_ANLAM:ZULZ_ARR_OJG:ZULZ_ARR_OTGIN:ZULZ_ARR_XYO:ZULZ_ARR_WLY:ZULZ_OTHER',
    ],
  ]));

  foreach ($payload as $i => $content) {
    $payload[$i]['step'] = param('NTFM_TMA_STEP_MINUTES');
    $payload[$i]['begTime'] = date('Y-m-d H', (time() - (3600 * param('NTFM_TMA_LOAD_HOURS_BEFORE')))) . ':00:00';
    $payload[$i]['endTime'] = date('Y-m-d H', (time() + (3600 * param('NTFM_TMA_LOAD_HOURS_AFTER')))) . ':00:00';
    $payload[$i]['class'] = 'DIY';
    $payload[$i]['filter'] = '';
    $payload[$i]['histUnEffect'] = 'Y';
    $payload[$i]['region'] = 'ZU_REGION';
    $payload[$i]['waitPool'] = 'N';
  }
  // param('NTFM_AP_PAYLOAD_LAST', $payload);
  return $payload;
}

function get_payload_nrs() {
  $payload = init_param('NTFM_NRS_PAYLOAD', json_stringify([
    'NORMAL_RATE_ITEMS' => ['ZUUU', 'ZUTF', 'ZUMY'],
    'NORMAL_RATE_TYPES' => ['flight', 'dep', 'tkf', 'arr', 'lnd', 'start', 'release'],
  ]));
  return $payload;
}